package com.walker.taskscheduling;

import com.banggood.sp.cloud.core.JsonUtils;
import com.banggood.sp.dispatcher.bean.TaskInfo;
import com.banggood.sp.dispatcher.core.TaskSource;
import com.walker.taskscheduling.bo.TaskDataBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.Resource;

@SpringBootApplication
public class TaskSchedulingApplication implements CommandLineRunner {

    @Resource
    private TaskSource firstTaskSource;
    public static void main(String[] args) {
        SpringApplication.run(TaskSchedulingApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.err.println("启动" + firstTaskSource);
        for (int i = 1; i <= 10; i++) {
            TaskDataBO taskDataBO = new TaskDataBO(String.valueOf(i % 2), String.valueOf(i), 0);
            TaskInfo taskInfo = new TaskInfo();
            // 设置任务key，key相同的任务会被去重掉
            taskInfo.setKey(taskDataBO.getProjectId());
            // 设置不同的时间戳
            taskInfo.setTimestamp(System.currentTimeMillis());
            // 设置不同的优先级
            taskInfo.setWeight(i);
            // 设置维度ID，当触发任务延时处理时，这一维度的任务都会被暂停
            taskInfo.setResourceId(taskDataBO.getProjectId());
            // 设置批量ID，当taskSource设置为批量消费时，批量ID相同的任务会被一起消费
            taskInfo.setBatchResourceId(taskDataBO.getProjectId());
            // 设置数据，即任务入参
            //taskInfo.setData("{\"changeSerialNumber\":1463416126763565110,\"createDate\":1637740568234,\"level\":2,\"listingId\":1396868722811731970,\"listingVariationId\":1396868725366063137,\"parentSellerSku\":\"POA738458\",\"parentShopSku\":\"\",\"pcStockList\":[{\"processCenterId\":722,\"quantity\":8},{\"processCenterId\":934,\"quantity\":0},{\"processCenterId\":7,\"quantity\":0},{\"processCenterId\":1111,\"quantity\":0},{\"processCenterId\":619,\"quantity\":0},{\"processCenterId\":1067,\"quantity\":0},{\"processCenterId\":1150,\"quantity\":0},{\"processCenterId\":1039,\"quantity\":0}],\"platform\":25,\"productId\":527990,\"propertyId\":760003,\"quantity\":8,\"sellerSku\":\"POA738458\",\"shopId\":3608,\"shopSku\":\"\",\"syncType\":0,\"type\":2}");
            taskInfo.setData(JsonUtils.writeValueAsString(taskDataBO));
            firstTaskSource.addTask(taskInfo);
        }
    }
}
